package com.xmsx.back.service.index;

import com.xmsx.back.mapper.index.GradMapper;
import com.xmsx.back.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class GradService {

    @Autowired
    GradMapper gradMapper;

    /**
     * 查询年份列表
     *
     * @return
     */
    public List<Integer> getYearList() {
        return gradMapper.getYearList();
    }

    /**
     * 查询指定起止年份的毕业去向数据并打包为ResultFirst类返回
     *
     * @param start
     * @param end
     * @return
     */
    public ResultFirst<Element> getDirectionData(int start, int end) {
        ResultFirst<Element> rf = new ResultFirst();
        rf.setStart(start);
        rf.setEnd(end);
        rf.setBachelor(gradMapper.getDirectionDataByYearsAndEdu(start, end, "本科生毕业"));
        rf.setMaster(gradMapper.getDirectionDataByYearsAndEdu(start, end, "硕士生毕业"));
        return rf;
    }

    /**
     * 查询指定起止年份的升学去向数据并打包为ResultSecond类返回
     *
     * @param start
     * @param end
     * @return
     */
    public ResultSecond getSchoolData(int start, int end) {
        ResultSecond rs = new ResultSecond();
        rs.setStart(start);
        rs.setEnd(end);
        rs.setDomestic(gradMapper.getSchoolDataByYearsAndBound(start, end, "境内升学"));
        rs.setForeign(gradMapper.getSchoolDataByYearsAndBound(start, end, "出国、出境"));
        return rs;
    }

    /**
     * 查询指定起止年份的单位性质数据并打包为ResultFirst类返回
     *
     * @param start
     * @param end
     * @return
     */
    public ResultFirst<Element> getOrgKindData(int start, int end) {
        ResultFirst<Element> rf = new ResultFirst();
        rf.setStart(start);
        rf.setEnd(end);
        rf.setBachelor(gradMapper.getOrgKindDataByYearsAndEdu(start, end, "本科生毕业"));
        rf.setMaster(gradMapper.getOrgKindDataByYearsAndEdu(start, end, "硕士生毕业"));
        return rf;
    }

    /**
     * 查询指定起止年份的单位行业数据并打包为ResultFirst类返回
     *
     * @param start
     * @param end
     * @return
     */
    public ResultFirst<Element> getOrgIndustryData(int start, int end) {
        ResultFirst<Element> rf = new ResultFirst();
        rf.setStart(start);
        rf.setEnd(end);
        rf.setBachelor(gradMapper.getOrgIndustryDataByYearsAndEdu(start, end, "本科生毕业"));
        rf.setMaster(gradMapper.getOrgIndustryDataByYearsAndEdu(start, end, "硕士生毕业"));
        return rf;
    }

    /**
     * 查询指定起止年份的就业单位数据并打包为ResultFirst类返回
     *
     * @param start
     * @param end
     * @return
     */
    public ResultFirst<Element> getOrgNameData(int start, int end) {
        ResultFirst<Element> rf = new ResultFirst();
        rf.setStart(start);
        rf.setEnd(end);
        rf.setBachelor(gradMapper.getOrgNameDataByYearsAndEdu(start, end, "本科生毕业"));
        rf.setMaster(gradMapper.getOrgNameDataByYearsAndEdu(start, end, "硕士生毕业"));
        return rf;
    }

    public List<ElementTotal> getOrgNameDataTotal(int start, int end) {
        List<ElementTotal> le = new LinkedList<>();
        le = gradMapper.getOrgNameDataByYears(start, end);
        for (int i = 0; i < le.size(); i++) {
            le.get(i).setBachelorNum(gradMapper.getOrgNameNumByYearsAndEduAndOrgName(start, end, "本科生毕业", le.get(i).getTerm()));
            le.get(i).setMasterNum(gradMapper.getOrgNameNumByYearsAndEduAndOrgName(start, end, "硕士生毕业", le.get(i).getTerm()));
            le.get(i).setTotal(le.get(i).getBachelorNum() + le.get(i).getMasterNum());
        }
        return le;
    }

    /**
     * 查询指定起止年份的就业地域数据并打包为ResultFirst类返回
     *
     * @param start
     * @param end
     * @return
     */
    public ResultFirst<Element> getOrgLocationData(int start, int end) {
        ResultFirst<Element> rf = new ResultFirst();
        rf.setStart(start);
        rf.setEnd(end);
        rf.setBachelor(gradMapper.getOrgLocationDataByYearsAndEdu(start, end, "本科生毕业"));
        rf.setMaster(gradMapper.getOrgLocationDataByYearsAndEdu(start, end, "硕士生毕业"));
        return rf;
    }

    /**
     * 查询所有年份的热门城市数据
     *
     * @return
     */
    public List<Element> getLatestHotCity() {
        List<Integer> yearList = getYearList();
        int startYear = yearList.get(0);
        int endYear = yearList.get(yearList.size() - 1);
        List<Element> le = gradMapper.getHotCity(startYear, endYear);
        return le;
    }

    /**
     * 按学历查询毕业生数量和就业率
     *
     * @param edu
     * @return
     */
    public List<Rate> getTotalAndRateByEdu(String edu) {
        List<Integer> yearList = getYearList();
        List<Rate> lir = new LinkedList<>();
        for (int i = 0; i < yearList.size(); i++) {
            Rate ir = new Rate();
            ir.setYear(yearList.get(i));
            ir.setNumber(gradMapper.countByYearAndEdu(ir.getYear(), edu));
            if (ir.getNumber() != 0)
                ir.setRate((double) gradMapper.countEmployedByYearAndEdu(ir.getYear(), edu) / ir.getNumber());
            lir.add(ir);
        }
        return lir;
    }

    /**
     * 查询本科生升学率数据
     *
     * @return
     */
    public List<Rate> getUpgradeBachelorRate() {
        List<Integer> yearList = getYearList();
        List<Rate> lir = new LinkedList<>();
        for (int i = 0; i < yearList.size(); i++) {
            Rate ir = new Rate();
            ir.setYear(yearList.get(i));
            ir.setNumber(gradMapper.countUpgradeByYearBachelor(ir.getYear()));
            ir.setRate((double) ir.getNumber() / gradMapper.countByYearAndEdu(ir.getYear(), "本科生毕业"));
            lir.add(ir);
        }
        return lir;
    }

    /**
     * 查询历年就业率数据
     *
     * @return
     */
    public List<Rate> getEmployedRate() {
        List<Integer> yearList = getYearList();
        List<Rate> lir = new LinkedList<>();
        for (int i = 0; i < yearList.size(); i++) {
            Rate ir = new Rate();
            ir.setYear(yearList.get(i));
            ir.setNumber(gradMapper.countEmployedByYearAndEdu(ir.getYear(), "本科生毕业") + gradMapper.countEmployedByYearAndEdu(ir.getYear(), "硕士生毕业"));
            ir.setRate((double) ir.getNumber() / (gradMapper.countByYearAndEdu(ir.getYear(), "本科生毕业") + gradMapper.countByYearAndEdu(ir.getYear(), "硕士生毕业")));
            lir.add(ir);
        }
        return lir;
    }

    /**
     * 按本科/硕士查询在鲁协议和合同就业率
     *
     * @param edu
     * @return
     */
    public List<Rate> getContractEmployedAtSDRateByEdu(String edu) {
        List<Integer> yearList = getYearList();
        List<Rate> lir = new LinkedList<>();
        for (int i = 0; i < yearList.size(); i++) {
            Rate ir = new Rate();
            ir.setYear(yearList.get(i));
            ir.setNumber(gradMapper.countContractEmployedAtSDByYearAndEdu(ir.getYear(), edu));
            ir.setRate((double) ir.getNumber() / (gradMapper.countContractEmployedByYearAndEdu(ir.getYear(), edu)));
            lir.add(ir);
        }
        return lir;
    }

    /**
     * 按起止日期查询导出就业质量报告所需数据
     *
     * @param start
     * @param end
     * @return
     */
    public DocumentData getDocumentDataByYear(int start, int end) {
        DocumentData documentData = new DocumentData();
        documentData.setBachelorNum(gradMapper.countByYearsAndEdu(start, end, "本科生毕业"));
        documentData.setMasterNum(gradMapper.countByYearsAndEdu(start, end, "硕士生毕业"));
        documentData.setMaleNum(gradMapper.countByYearsAndSex(start, end, "男"));
        documentData.setFemaleNum(gradMapper.countByYearsAndSex(start, end, "女"));
        documentData.setTotal(documentData.getBachelorNum() + documentData.getMasterNum());
        double rate = (double) gradMapper.countBackToOriginNumByYears(start, end) / gradMapper.countContractEmployedByYears(start, end);
        documentData.setBackToOriginRate(rate);
        return documentData;
    }

    /**
     * 按起止日期查询回生源地就业数据
     *
     * @param start
     * @param end
     * @return
     */
    public List<Rate> getBackToOriginData(int start, int end) {
        List<Rate> lr = new LinkedList<>();
        Rate domestic = new Rate();
        domestic.setYear(0);
        domestic.setNumber(gradMapper.countContractEmployedByYearsSDer(start, end));
        domestic.setRate((double) gradMapper.countContractEmployedAtSDByYearsSDer(start, end) / domestic.getNumber());
        lr.add(domestic);
        Rate foreign = new Rate();
        foreign.setYear(1);
        foreign.setNumber(gradMapper.countContractEmployedByYearsUnSDer(start, end));
        foreign.setRate((double) gradMapper.countContractEmployedBackToOriginByYearsUnSDer(start, end) / domestic.getNumber());
        lr.add(foreign);
        return lr;
    }
}
